package com.socket.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class ServerThread extends Thread {
	
	private Socket socket;

    //在构造中得到要单独会话的socket
    public ServerThread(Socket socket) {
        this.socket = socket;
    }
    
    @Override
    public void run() {
    	super.run();
        InputStreamReader reader = null;
        BufferedReader bufReader = null;
        OutputStream os = null; 
        OutputStream out = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
        FileUtil.createDirFile("/temp");
        Random random = new Random();
        DecimalFormat df = new DecimalFormat("000000");
//        File file = FileUtil.createFile("/temp/temp_" + sdf.format(new Date()) + "_" + df.format(random.nextInt(999999)) + ".txt");
        File file = FileUtil.createFile("/temp/temp.csv");
        try {
        	InputStream is = socket.getInputStream();
        	reader = new InputStreamReader(is, "ASCII");
            bufReader = new BufferedReader(reader);
            String s = null;
            StringBuffer sb = new StringBuffer();
//            Writer out = new FileWriter(file);
            out = new FileOutputStream(file, true);
            byte[] bs = new byte[1024];
            int len = 0;
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS ");
          //返回给客户端数据
            os = socket.getOutputStream();
//            os = socket.getOutputStream();
          
//            os.write("保存成功：".getBytes());
            
            while((len = is.read(bs)) != -1){
//                out.write(s);
            	out.write(sdf2.format(new Date()).getBytes());
                out.write(bs, 0, len);
                out.write((" " + len).getBytes());
                out.write(",".getBytes());
//                os.write(stringToA("*******************************************************************************\n").getBytes());
//                os.write(stringToA("保存成功").getBytes());
//                os.write(stringToA("*******************************************************************************\n").getBytes());
//                os.write(("*******************************************************************************\n").getBytes());
                os.write(("保存成功").getBytes());
//                os.write(("*******************************************************************************\n").getBytes());
                os.flush();
            }
//            is.close();
//            System.out.println("服务器："+sb.toString());
            out.flush();
            //关闭输入流
            socket.shutdownInput();
            
            
            socket.shutdownOutput();
        } catch (IOException e2) {
            e2.printStackTrace();
            try {
            	if (out == null) {
            		out = new FileOutputStream(file);
				}
            	out.write(e2.getLocalizedMessage().getBytes());
			} catch (IOException e) {
				e.printStackTrace();
			}
        } finally{//关闭IO资源
        	
        	try {
        		if (out != null) {
            		out.close();
            		out = null;
    			}
        		if(reader != null){
        			 reader.close();
        			 reader = null;
                }
        		if(bufReader != null){
        			bufReader.close();
        			bufReader = null;
                }
                if(os != null){
                	os.close();
                	os = null;
                }
//                socket.close();
//                socket = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
            
        }
         
          
    }
    
  //字符串转换为ascii
    public static String stringToA(String content){
      String result = "";
      int max = content.length();
      for (int i=0; i<max; i++){
        char c = content.charAt(i);
        int b = (int)c;
        result = result + b;
      }
      return result;
    }

}
